package com.china.milkywayeye.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.china.milkywayeye.pojo.PanoramFolder;
import com.china.milkywayeye.service.PanoramaService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName PanoramaServiceImpl
 * @Description TODO
 * @Author milkywayeye
 * @Date 2024/1/3 11:40
 * @Version V1.0
 **/
@Service
public class PanoramaServiceImpl implements PanoramaService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    private BufferedReader reader;

    private PanoramFolder panoramFolder;


    @Override
    public void addClick(Integer page, Integer posNum) {
        downloadAnUpload(page, posNum, "onBottom", "");
    }

    @Override
    public void addClickLine(Integer page, Integer posNum) {
        downloadAnUpload(page, posNum, "onBottom2", "");
    }

    @Override
    public void addHotPoint(Integer page, Integer posNum, String s) {
        downloadAnUpload(page, posNum, "onTop", s);
    }

    public void downloadAnUpload(Integer page, Integer posNum, String type, String s) {
        OSS ossClient = null;
        try {
            String FilePath = "./test/";
            File file = new File(FilePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint
                    , accessKeyId
                    , accessKeySecret);
            DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, "panoramicImages/GuangMingPanoramicImage" + page + "/D" + posNum + "00/pano.xml");
            downloadFileRequest.setDownloadFile(FilePath + "pano.xml");
            // 下载文件。
            DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
            // 下载成功时，会返回文件元信息。
            ObjectMetadata objectMetadata = downloadRes.getObjectMetadata();

            if (Objects.equals(type, "onBottom")) {
                onBottom();
            } else if (Objects.equals(type, "onTop")) {
                onTop(s);
            } else if (Objects.equals(type, "onBottom2")) {
                onBottom2();
            } else {
                return;
            }

            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, "panoramicImages/GuangMingPanoramicImage" + page + "/D" + posNum + "00/pano.xml");
            uploadFileRequest.setUploadFile("./test/pano1.xml");
            ossClient.uploadFile(uploadFileRequest);
        } catch (Exception e) {
            System.out.println("下载有问题");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            //资源关闭
            assert ossClient != null;
            ossClient.shutdown();
        }
        System.out.println("上传下载完毕");
    }

    //在指定字符串上一行插入内容
    public void onTop(String s) throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "</krpano>";
        String insertContent = s;
        String line;
        String prevLine = null;
        while ((line = reader.readLine()) != null) {
            if (prevLine != null) {
                writer.write(prevLine);
                writer.newLine();
            }
            prevLine = line;
            if (line.contains(searchStr)) {
                // 找到</krpano>所在行的上一行位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
        }

        writer.write(prevLine); // 写入文件的最后一行

        reader.close();
        writer.close();
    }


    //在指定字符串下一行插入内容
    public void onBottom() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "<include url=\"skin/defaultskin.xml\" />";
        String insertContent = "<include url=\"../../GuangMingPanoramicImage32/D800/skin/getpos.xml\"/>";
        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
            if (line.contains(searchStr)) {
                // 找到</krpano>所在行的下一行位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
        }

        reader.close();
        writer.close();
    }

    //在指定字符串下一行插入内容-内容为划线插件
    public void onBottom2() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "<include url=\"skin/defaultskin.xml\" />";
        String insertContent = "<include url=\"../../GuangMingPanoramicImage69/my-plugin.xml\"/>";
        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
            if (line.contains(searchStr)) {
                // 找到</krpano>所在行的下一行位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
        }

        reader.close();
        writer.close();
    }

    //上传一整个文件夹
    public void uploadFolder() {
        OSS ossClient = null;
        try {
//            String FilePath = "./test/";
//            File file = new File(FilePath);
//            if (!file.exists()) {
//                file.mkdirs();
//            }
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint
                    , accessKeyId
                    , accessKeySecret);
//            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, "panoramicImages/");
//            uploadFileRequest.setUploadFile("./test/pano1.xml");
//            ossClient.uploadFile(uploadFileRequest);
//            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, "panoramicImages/test/");
//            uploadFileRequest.setUploadFile("./test");
//            ossClient.uploadFile(uploadFileRequest);
            // 获取文件列表
            List<File> fileList = getFileList(new File("D:\\space_4\\wurenji\\1\\image\\qishu\\test"), new ArrayList<>());
            // 遍历文件列表
            for (File file : fileList) {
                // 创建上传请求
                String key = "panoramicImages/test/" + file.getName();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
                // 执行上传请求
                ossClient.putObject(putObjectRequest);
            }
        } catch (Exception e) {
            System.out.println("有问题");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            //资源关闭
            assert ossClient != null;
            ossClient.shutdown();
        }
        System.out.println("上传完毕");
    }

    //获取本地文件夹中文件列表-单层
    private List<File> getFileList(File dir, List<File> fileList) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                getFileList(file, fileList);
            } else {
                fileList.add(file);
            }
        }
        return fileList;
    }

    //获取本地文件中文件对象-多层
//    private PanoramFolder getFileList(File dir, PanoramFolder panoramFolder) {
//        File[] files = dir.listFiles();
//        assert files != null;
//        for (File file : files) {
//            if (file.isDirectory()) {
//
//            } else {
//                List<PanoramFolder> list = panoramFolder.getChilds();
//                list.add(new PanoramFolder().setLeafFlag(true).setFile(file).setFileName(file.getName()));
//                panoramFolder.setChilds(list);
//            }
//        }
//        return panoramFolder;
//    }
}
